package com.library.common.toast;

import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.library.common.R;
import com.library.common.base.BaseApplication;


/**
 * 自定义Toast
 */
public class XToast {

    private static View toastLayout = null;

    private XToast() {
    }

    public static void normal(@NonNull String message) {
        custom(message, null);
    }

    public static void warning(@NonNull String message) {
        custom(message, ContextCompat.getDrawable(BaseApplication.Companion.getInstance(), R.drawable.icon_fanmang));
    }

    public static void success(@NonNull String message) {
        custom(message, ContextCompat.getDrawable(BaseApplication.Companion.getInstance(), R.drawable.icon_chenggong));
    }

    public static void error(@NonNull String message) {
        custom(message, ContextCompat.getDrawable(BaseApplication.Companion.getInstance(), R.drawable.icon_shibai));
    }

    /**
     * 自定义toast方法
     *
     * @param message 提示消息文本
     * @param icon    提示消息的icon,传入null代表不显示
     * @return
     */
    public static void custom(@NonNull String message, Drawable icon) {
        toastLayout = LayoutInflater.from(BaseApplication.Companion.getInstance()).inflate(R.layout.xtoast_view, null);
        ImageView toastIcon = toastLayout.findViewById(R.id.xtoast_icon);
        if (icon == null) {
            toastIcon.setVisibility(View.GONE);
        } else {
            toastIcon.setImageDrawable(icon);
        }
        ToastUtils.setView(toastLayout);
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.show(message);
    }
}
